package com.qianfeng.biquge.service.impl;

import com.qianfeng.biquge.constans.ResultCode;
import com.qianfeng.biquge.dto.UserDto;
import com.qianfeng.biquge.exceptions.AddDataException;
import com.qianfeng.biquge.exceptions.QueryDataException;
import com.qianfeng.biquge.exceptions.UpdateDataException;
import com.qianfeng.biquge.mapper.UserMapper;
import com.qianfeng.biquge.pojo.Users;
import com.qianfeng.biquge.service.IUserService;
import com.qianfeng.biquge.utils.CheckSaltAndMD5Util;
import com.qianfeng.biquge.utils.DateZone8Util;
import com.qianfeng.biquge.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
@Transactional
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public void addUser(UserDto userDto) {

        if (!userDto.getPassword().equals(userDto.getRepeatpassword())){

            throw new UpdateDataException("添加数据失败,密码不一致", ResultCode.PASSWORD_NO_LIKE);

        }

        List<Users> allUser = userMapper.getAllUser();
        String passWordSalt = null;
        String md5 = null;
        boolean b = false;

        //盐和MD5重复性校验
        while (true){

            if (!b){

                Map<String, String> map1 = MD5.enMD5(userDto.getPassword());
                passWordSalt = map1.get("passWordSalt");
                md5 = map1.get("md5");

                b = CheckSaltAndMD5Util.checkSaltAndMD5(allUser, passWordSalt, md5);

            }else {

                break;

            }

        }

        if (passWordSalt==null || md5==null){

            throw new UpdateDataException("MD5不能为空", ResultCode.NULL_DATA);

        }
        userDto.setPasswordsalt(passWordSalt);
        userDto.setPassword(md5);

        userMapper.addUser(userDto);

    }

    @Override
    public Users login(UserDto userDto) throws ParseException {

        if (userDto==null || userDto.getUsername()==null || userDto.getUsername().equals("")|| userDto.getPassword()==null || userDto.getPassword().equals("")){

            throw new QueryDataException("账号或密码未传递", ResultCode.NULL_USERNAME);

        }

        //查询用户
        Users users = userMapper.getUserByuserName(userDto);

        if (users==null){

            throw new QueryDataException("用户名不存在", ResultCode.USERNAME_OR_PASSWORD_ERROR);

        }

        boolean b = MD5.deMD5(userDto.getPassword(), users.getPasswordsalt(), users.getPassword());

        if (!b){

            throw new QueryDataException("用户名或密码错误",ResultCode.USERNAME_OR_PASSWORD_ERROR);

        }


        users.setLastlogindate(users.getCurrentlogindate());
        if (users.getLastlogindate()==null){

            users.setLastlogindate(new Date());

        }
        users.setCurrentlogindate(new Date());

        //更新登录时间
        int i = userMapper.updateUser(users);

        if (i<=0){

            throw new UpdateDataException("修改数据库登录时间失败", ResultCode.NULL_DATA);

        }

        //因为转换成json之后,时间会少8小时,所以加8小时,仅用于显示

        Date lastLoginDate = DateZone8Util.add8Hour(users.getLastlogindate().getTime());
        Date currentLoginDate = DateZone8Util.add8Hour(users.getCurrentlogindate().getTime());

        users.setLastlogindate(lastLoginDate);
        users.setCurrentlogindate(currentLoginDate);
        return users;

    }
}
